if (!TrvnTaskManager) {

	var TrvnTaskManager = {
		BUILD_TYPE : 1,
		ACTIVITY_TYPE : 1 << 1,
		RESEARCH_DEV_TYPE : 1 << 2,
		RESEARCH_ARMOUR_TYPE : 1 << 3,
		RESEARCH_ATTACK_TYPE : 1 << 4,
		RESOURCE_TRANSPORT_TYPE : 1 << 5,
		ARMY_MOVE_TYPE : 1 << 6,
		DESTROY_TYPE : 1 << 7,
		toTaskFinishString : function(vlg, type, task) {
			type = TrvnUtils.parseNumber(type);
			var vlgTitle = '村庄 ' + vlg.title;
			var actionTitle;
			switch (type) {
				case this.RESEARCH_DEV_TYPE :
					actionTitle = ' 研发所 已完成 ';
					break;
				case this.RESEARCH_ARMOUR_TYPE :
					actionTitle = ' 军械库 已完成 ';
					break;
				case this.RESEARCH_ATTACK_TYPE :
					actionTitle = ' 铁匠铺 已完成 ';
					break;
				case this.DESTROY_TYPE :
					actionTitle = ' 已拆毁建筑 ';
					break;
				case this.RESOURCE_TRANSPORT_TYPE :
				case this.ARMY_MOVE_TYPE :
				case this.BUILD_TYPE :
				case this.ACTIVITY_TYPE :
				default :
					actionTitle = ' 已完成 ';
			}
			return vlgTitle + actionTitle + task.titles[0];
		},
		finishTask : function(thisVlg, thisTaskType, thisTaskId, thisTask) {
			try {
				var txt = Travian.toolDoc.$C("span");
				txt.className = "c" + Travian.toolDoc.$G("evnt").childNodes.length % 7;
				var s = TrvnTaskManager.toTaskFinishString(thisVlg, thisTaskType, thisTask);
				txt.innerHTML = s;
				Travian.toolDoc.addListPaneItem("evnt", txt);
				if (TrvnCfg.alertOnTaskFinish) {
					alert(s);
				}
				thisVlg.tasks[thisTaskType][thisTaskId] = undefined;
			} catch (e) {
				TrvnUtils.Logger.error("task-manager.js 401: " + e);
			}
		},
		scanTasksInterval : 0,
		scanTasks : function(scanOnly) {
			try {
				TrvnUtils.each(TrvnCfg.accounts, function(accountName, account) {
					TrvnUtils.each(account.villages, function(villageName, village) {
						TrvnUtils.each(village.tasks, function(taskType, tasks) {
							TrvnUtils.each(tasks, function(taskId, task) {
								if (!task) return;
								var t = task.finishTime - new Date().getTime();
								if (t > 5 * 60 * 1000) {
									if (task.timer && task.timer > 0) {
										clearTimeout(task.timer);
										task.timer = 0;
									}
									return;
								} else if (task.timer && task.timer > 0) {
									if (scanOnly) {
										return;
									} else {
										clearTimeout(task.timer);
										task.timer = 0;
									}
								}

								task.timer = setTimeout(TrvnTaskManager.finishTask, t + 10 * 1000, village, taskType, taskId, task);
							});
						});
					});
				});
			} catch (e) {
				TrvnUtils.Logger.error("task-manager.js 402: " + e);
			}
		},
		startScanTasksInterval : function() {
			this.scanTasksInterval = setInterval(TrvnTaskManager.scanTasks, 4 * 60 * 1000, true);
		},
		stopScanTasksInterval : function() {
			clearInterval(this.scanTasksInterval);
		},

		getVillage : function(vlg) {
			return vlg ? vlg : TrvnCfg.getCurrentVillage();
		},

		addTask : function(vlg, type, id, titles, finishTime) {
			if (!vlg.tasks) {
				vlg.tasks = [];
			}
			if (!vlg.tasks[type]) {
				vlg.tasks[type] = [];
			}
			var task = {
				titles : titles,
				finishTime : finishTime,
				timer : 0
			};
			var timeLeft = finishTime - new Date().getTime();
			if (timeLeft <= 5 * 60 * 1000) {
				task.timer = setTimeout(TrvnTaskManager.finishTask, timeLeft, vlg, type, id, task);
			}
			vlg.tasks[type][id] = task;
		},
		clearTask : function(vlg, type) {
			if (vlg.tasks && vlg.tasks[type]) {
				TrvnUtils.each(vlg.tasks[type], function(taskName, task) {
					if (task && task.timer) {
						clearTimeout(task.timer);
					}
				});
				vlg.tasks[type] = undefined;
			}
		},

		addBuildTask : function(id, title, finishTime, vlg) {
			this.addTask(this.getVillage(vlg), this.BUILD_TYPE, id, [title], finishTime);
		},
		clearBuildTask : function(vlg) {
			this.clearTask(this.getVillage(vlg), this.BUILD_TYPE);
		},

		addDestroyTask : function(title, finishTime, vlg) {
			this.addTask(this.getVillage(vlg), this.DESTROY_TYPE, title, [title], finishTime);
		},
		clearDestroyTask : function(vlg) {
			this.clearTask(this.getVillage(vlg), this.DESTROY_TYPE);
		},

		addActivityTask : function(title, finishTime, vlg) {
			this.addTask(this.getVillage(vlg), this.ACTIVITY_TYPE, "activity", [title], finishTime);
		},
		clearActivityTask : function(vlg) {
			this.clearTask(this.getVillage(vlg), this.ACTIVITY_TYPE);
		},

		addResearchDevTask : function(what, finishTime, vlg) {
			this.addTask(this.getVillage(vlg), this.RESEARCH_DEV_TYPE, "academy", [what], finishTime);
		},
		clearResearchDevTask : function(vlg) {
			this.clearTask(this.getVillage(vlg), this.RESEARCH_DEV_TYPE);
		},

		addResearchArmourTask : function(what, finishTime, vlg) {
			this.addTask(this.getVillage(vlg), this.RESEARCH_ARMOUR_TYPE, "armoury", [what], finishTime);
		},
		clearResearchArmourTask : function(vlg) {
			this.clearTask(this.getVillage(vlg), this.RESEARCH_ARMOUR_TYPE);
		},

		addResearchAttackTask : function(what, finishTime, vlg) {
			this.addTask(this.getVillage(vlg), this.RESEARCH_ATTACK_TYPE, "blacksmith", [what], finishTime);
		},
		clearResearchAttackTask : function(vlg) {
			this.clearTask(this.getVillage(vlg), this.RESEARCH_ATTACK_TYPE);
		}
	};

}